package com.sgb.kjwl.view.ui.fragment.manage;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;

import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FrOperatingBusinessForWebsiteBinding;
import com.sgb.kjwl.di.manage.DaggerEnterpriseManageFragComponent;
import com.sgb.kjwl.di.manage.EnterpriseManageFragModule;
import com.sgb.kjwl.view.ui.fragment.home.WebSiteGoodsListFragment;
import com.sgb.kjwl.view.ui.fragment.packers.TeamServiceFragment;
import com.sgb.kjwl.view.ui.fragment.shop.ShopMachineChildFragment;
import com.sgb.kjwl.view.ui.fragment.transport.CarsFragment;
import com.sgb.kjwl.viewmodel.manage.OperatingBusinessForWebsiteModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;
import com.swgk.core.util.PickSelectEntity;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/*
 * Author LiWei
 * Create Time 2021/4/15
 * Description 网站-经营业务
 * */
public class OperatingBusinessForWebsite extends AppLazyFragment {

    @Inject
    OperatingBusinessForWebsiteModel viewModel;
    private FrOperatingBusinessForWebsiteBinding binding;

    private String websiteNo;
    private String websiteType;
    private int childPosition;
    private List<String> mTitles = new ArrayList<>();
    private ArrayList<Fragment> mFragments = new ArrayList<>();

    private String[] tabMenu = {"商品经营", "租赁经营", "集采经营", "工队经营", "企服经营", "物流经营"};
    private String[] tabMenu1 = {"租赁经营", "集采经营", "工队经营", "企服经营", "物流经营"};

    private boolean isMyself;
    public static final String TYPE_IS_MYSELF = "type_is_myself"; //是不是当前身份的网站

    public static OperatingBusinessForWebsite newInstance(String websiteNo, int websiteType, int childPosition, boolean isMySelf) {
        OperatingBusinessForWebsite fragment = new OperatingBusinessForWebsite();
        Bundle args = new Bundle();
        args.putString("websiteNo", websiteNo);
        args.putString("websiteType", String.valueOf(websiteType));
        args.putInt("childPosition", childPosition);
        args.putBoolean(TYPE_IS_MYSELF, isMySelf);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        super.setupFragmentComponent(appComponent);
        DaggerEnterpriseManageFragComponent.builder().appComponent(appComponent)
                .enterpriseManageFragModule(new EnterpriseManageFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fr_operating_business_for_website, container, false);
        if (getArguments() != null) {
            websiteNo = getArguments().getString("websiteNo");
            websiteType = getArguments().getString("websiteType");
            childPosition = getArguments().getInt("childPosition");
            isMyself = getArguments().getBoolean(TYPE_IS_MYSELF);
        }
        return binding.getRoot();
    }

    @Override
    protected void onLazyLoad() {
        if (!isMyself) {
            viewModel.getSecondMenus(websiteNo, websiteType);
        } else {
            initTabLayout();
        }
    }

    @Override
    protected void initView() {
        super.initView();
        if (!isMyself) {
            viewModel.mMenus.observe(this, new Observer<List<PickSelectEntity>>() {
                @Override
                public void onChanged(List<PickSelectEntity> pickSelectEntities) {
                    if (pickSelectEntities != null) {
                        initTabLayout(pickSelectEntities);
                    }
                }
            });
        }
    }

    private void initTabLayout() {
        ArrayList<Fragment> mFragments = new ArrayList<>();
        if ("1".equals(websiteType)) {
            mFragments.add(WebSiteGoodsListFragment.newInstance(websiteNo));
        }
        mFragments.add(ShopMachineChildFragment.newInstance(websiteNo, websiteType, 0));//租赁经营
        mFragments.add(JiCaiManageFragment.newInstance(websiteNo, 0));//集采经营
        mFragments.add(TeamServiceFragment.newInstance(websiteNo));
        mFragments.add(EnterpriseServicesFragment.newInstance(websiteNo, websiteType, 0));
        mFragments.add(CarsFragment.newInstance(websiteType,websiteNo));
        SlidingFragmentPagerAdapter mAdapter = new SlidingFragmentPagerAdapter(getChildFragmentManager(), mFragments, "1".equals(websiteType) ? tabMenu : tabMenu1);
        binding.viewpager.setAdapter(mAdapter);
        binding.viewpager.setOffscreenPageLimit("1".equals(websiteType) ? tabMenu.length : tabMenu1.length);
        binding.rvChildTabLayout.setupWithViewPager(binding.viewpager);
        binding.viewpager.setCurrentItem(childPosition);//跳转具体界面,勿删待定
        setTabLayoutListener();
    }

    private void setTabLayoutListener() {
        binding.rvChildTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int tabCount = binding.rvChildTabLayout.getTabCount();
                for (int i = 0; i < tabCount; i++) {
                    if (binding.rvChildTabLayout.getTabAt(i) != null && binding.rvChildTabLayout.getTabAt(i).getCustomView() != null
                            && binding.rvChildTabLayout.getTabAt(i).getCustomView().findViewById(R.id.tv_name) != null)
                        setTabStatus(i == tab.getPosition(), binding.rvChildTabLayout.getTabAt(i).getCustomView().findViewById(R.id.tv_name));
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        initCustomizeTab();
    }


    private void initTabLayout(List<PickSelectEntity> titles) {
        mFragments.clear();
        mTitles.clear();
        for (PickSelectEntity entity : titles) {
            if (entity.getProjectscale().equals("商品经营")) { //商品经营
                mFragments.add(WebSiteGoodsListFragment.newInstance(websiteNo));
            }
            if (entity.getProjectscale().equals("租赁经营")) { //租赁经营
                mFragments.add(ShopMachineChildFragment.newInstance(websiteNo, websiteType, 0));//租赁经营
            }
            if (entity.getProjectscale().equals("集采经营")) { //集采经营
                mFragments.add(JiCaiManageFragment.newInstance(websiteNo, 0));//集采经营
            }
            if (entity.getProjectscale().equals("工队经营")) { //工队经营
                mFragments.add(TeamServiceFragment.newInstance(websiteNo));
            }
            if (entity.getProjectscale().equals("企服经营")) { //企服经营
                mFragments.add(EnterpriseServicesFragment.newInstance(websiteNo, websiteType, 0));
            }
            if (entity.getProjectscale().equals("物流经营")){ //物流经营
                mFragments.add(CarsFragment.newInstance(websiteType,websiteNo));
            }
            mTitles.add(entity.getProjectscale());
        }

        SlidingFragmentPagerAdapter mAdapter = new SlidingFragmentPagerAdapter(getChildFragmentManager(), mFragments, mTitles.toArray(new String[mTitles.size()]));
        binding.viewpager.setAdapter(mAdapter);
        binding.viewpager.setOffscreenPageLimit(mTitles.size());
        binding.rvChildTabLayout.setupWithViewPager(binding.viewpager);
        binding.viewpager.setCurrentItem(childPosition);//跳转具体界面,勿删待定
        setTabLayoutListener();
    }

    private void initCustomizeTab() {
        int total = binding.rvChildTabLayout.getTabCount();
        View mTabCustomerView;
        for (int i = 0; i < total; i++) {
            if (binding.rvChildTabLayout.getTabAt(i) != null) {
                mTabCustomerView = LayoutInflater.from(getContext()).inflate(R.layout.item_tablayout_customize_website_second_view, null);
                TextView tvName = mTabCustomerView.findViewById(R.id.tv_name);
                tvName.setText(binding.rvChildTabLayout.getTabAt(i).getText());
                binding.rvChildTabLayout.getTabAt(i).setCustomView(mTabCustomerView);
                setTabStatus(i == Math.min(total - 1, childPosition), tvName);
            }
        }
    }

    /**
     * @param isChoose 选择的tab
     * @param tvName   tab样式
     */
    private void setTabStatus(boolean isChoose, TextView tvName) {
        tvName.setSelected(isChoose);
        tvName.setTextColor(getResources().getColor(isChoose ? R.color.color_main : R.color.color_tab_three));
    }
}
